#include "../../Base/stdafx.h"
#include "../../Base/GameEngine.h"
#include "../../Base/DXDevice.h"
#include "Grid.h"
#include "../../Base/Vertex.h"

Grid::Grid()
{
	Init();
}

Grid::~Grid()
{
	if(m_VertexBuffer) m_VertexBuffer->Release();
	if(m_Layout) m_Layout->Release();
	if(m_Effect) m_Effect->Release();
}

void Grid::Draw()
{
	DXDEVICE->GetMatrixen(m_World, m_View, m_Proj);
	DXDevice::GetSingleton()->GetDevice()->IASetInputLayout(m_Layout);
	UINT stride = sizeof(VertexPC);
	UINT offset = 0;
	m_WorldVar->SetMatrix((float*)&m_World);
	m_ViewVar->SetMatrix((float*)&m_View);
	m_ProjVar->SetMatrix((float*)&m_Proj);

	DXDevice::GetSingleton()->GetDevice()->IASetVertexBuffers(0, 1, &m_VertexBuffer, &stride, &offset);
	DXDevice::GetSingleton()->GetDevice()->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_LINELIST);

	D3D10_TECHNIQUE_DESC tech;
	m_Tech->GetDesc( &tech);
	for(UINT t = 0; t < tech.Passes; ++t)
	{
		m_Tech->GetPassByIndex(t)->Apply(0);
		DXDEVICE->GetDevice()->Draw(2000,0);
	}
}

void Grid::Init()
{
	DWORD ShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined(_DEBUG) || defined(DEBUG)
	ShaderFlags |= D3D10_SHADER_DEBUG;
#endif
	HRESULT hr = D3DX10CreateEffectFromFile(_T("./Shaders/PosCol.fx"), NULL, NULL, "fx_4_0", ShaderFlags, NULL, DXDevice::GetSingleton()->GetDevice(), NULL, NULL, &m_Effect, NULL, NULL);
	if(FAILED(hr))
	{
		MessageBox(NULL, _T("Kan Effect niet aanmaken"), NULL, MB_OK);
		return;
	}

	m_Tech = m_Effect->GetTechniqueByName("Render");
	m_WorldVar = m_Effect->GetVariableByName("World")->AsMatrix();
	m_ViewVar = m_Effect->GetVariableByName("View")->AsMatrix();
	m_ProjVar = m_Effect->GetVariableByName("Projection")->AsMatrix();

#pragma region VertexLayout
	D3D10_INPUT_ELEMENT_DESC Layout[] =
	{
		{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
		{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0},
	};

	UINT numElements = sizeof(Layout) / sizeof(Layout[0]);
	
	D3D10_PASS_DESC passDesc;
	m_Tech->GetPassByIndex(0)->GetDesc(&passDesc);
	hr = DXDevice::GetSingleton()->GetDevice()->CreateInputLayout(Layout, numElements, passDesc.pIAInputSignature, passDesc.IAInputSignatureSize,  &m_Layout);
	if(FAILED(hr))
	{
		MessageBox(NULL, _T("ERROR layout"), NULL, MB_OK);
		return;
	}
#pragma endregion
#pragma region VertexBuffer
	
	// Create vertex buffer
    VertexPC v[2000];

	int i=0;
	//red X axis
	for(int teller = -250; teller < 250; teller += 1)
	{
		v[i++] = VertexPC(-250.5f, 0.02f, (float)teller + 0.5f,D3DXVECTOR4(0,0,0, 1));
		v[i++] = VertexPC(250.5f, 0.02f, (float)teller + 0.5f,D3DXVECTOR4(0,0,0, 1));
	}
	//blue Z axis
	for(int teller = -250; teller < 250; teller += 1)
	{
		v[i++] = VertexPC( (float)teller + 0.5f, 0.02f,  250.5f,D3DXVECTOR4(0,0,0, 1));
		v[i++] = VertexPC( (float)teller + 0.5f, 0.02f, -250.5f,D3DXVECTOR4(0,0,0, 1));
	}
	
	D3D10_BUFFER_DESC bd;
    bd.Usage = D3D10_USAGE_DEFAULT;
    bd.ByteWidth = sizeof( VertexPC ) * 2000;
    bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
    bd.CPUAccessFlags = 0;
    bd.MiscFlags = 0;
    D3D10_SUBRESOURCE_DATA InitData;
    InitData.pSysMem = v;
	hr = DXDevice::GetSingleton()->GetDevice()->CreateBuffer( &bd, &InitData, &m_VertexBuffer );
    if( FAILED( hr ) )
	{
		MessageBox(NULL, _T("ERROR creating vertex buffer"), NULL, MB_OK);
        return;
	}
#pragma endregion
}

